package com.aidex.biz.smy.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class MessageSign {

    public static final Logger logger = LoggerFactory.getLogger(MessageSign.class);

    public static final Charset CHARSET = Charset.forName("utf-8");

    public static final String ALGORITHMS_SHA1 = "SHA-1";

    public static final String ALGORITHMS_MD5 = "MD5";

    /**
     * 消息摘要，使用参数 algorithms 指定的算法
     *
     * @param algorithms
     * @param inputStr
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static byte[] sign(String algorithms, String inputStr) throws NoSuchAlgorithmException {
        MessageDigest messageDigest = MessageDigest.getInstance(algorithms);
        messageDigest.update(inputStr.getBytes(CHARSET));
        return messageDigest.digest();
    }

    /**
     * byte 数组转 十六进制字符串
     *
     * @param byteArray
     * @return
     */
    public static String byte2HexStr(byte[] byteArray) {

        char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        char[] resultCharArray = new char[byteArray.length * 2];
        int index = 0;
        for (byte b : byteArray) {
            resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
            resultCharArray[index++] = hexDigits[b & 0xf];
        }

        return new String(resultCharArray);
    }

    /**
     * 签名,并将签名结果转换成 十六进制字符串
     *
     * @param algorithms
     * @param inputStr
     * @return
     */
    public static String signToHexStr(String algorithms, String inputStr) {
        try {
            return byte2HexStr(sign(algorithms, inputStr));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String signParams(String key, String sourceStr) {
        String md5Str1 = signToHexStr(ALGORITHMS_MD5, sourceStr);
        String sourceStr2 = md5Str1 + key;
        return signToHexStr(ALGORITHMS_MD5, sourceStr2);
    }

    /**
     *
     * 仅作为测试用途，具体加密流程以接口文档为准
     *
     * @return
     */
    public static String signTest() {
        String secretKey = "19b113c947334b68b7b4c1c658e33f3a";
        String mobileNo = "13717566001";
        String mobileNoMd5 = signToHexStr(ALGORITHMS_MD5,mobileNo);
        //手机号的md5值,不需要解密,直接撞库
        System.out.println("mobile-md5:" + mobileNoMd5);
        System.out.println("sign:" + signParams(secretKey, mobileNoMd5));
        return null;
    }

    public static void main(String[] args) {
        String secretKey = "1be335a0feec4d18";
        String mobileNo = "13603281239";
        String mobileNoMd5 = signToHexStr(ALGORITHMS_MD5,mobileNo);
        //手机号的md5值,不需要解密,直接撞库
        System.out.println("mobile:" + mobileNo);
        System.out.println("mobile-md5:" + mobileNoMd5);
        System.out.println("sign:" + signParams(secretKey, mobileNoMd5));
    }

}
